package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes(names = {"msg"})
public class TestController {
    @RequestMapping("/resp1")
    public String resp1(HttpServletRequest request){
        request.setAttribute("msg","请求域中的数据");
        return "/index.jsp";
    }
    @RequestMapping("/resp2")
    public String resp2(Model model){
        model.addAttribute("msg","请求域中的数据");
        return "/index.jsp";
    }
    @RequestMapping("/resp3")
    public String resp3(ModelMap modelMap){
        //modelMap.addAttribute("msg","请求域中的数据");
        modelMap.put("msg","请求域中的数据");
        return "/index.jsp";
    }
    //模式和视图对象：既可以在请求域中添加数据，也可以跳转到视图界面
    @RequestMapping("/resp4")
    public ModelAndView resp4(ModelAndView mav){
        //在请求域中添加对象
        mav.addObject("msg","ModelAndView");
        //指定视图页面在哪儿
        mav.setViewName("/index.jsp");
        return mav;
    }
    @RequestMapping("/resp5")
    public String resp5(ModelMap modelMap){
        //modelMap.addAttribute("msg","请求域中的数据");
        modelMap.put("msg","session域中的数据");
        return "redirect:/index.jsp";
    }
    @RequestMapping("/resp6")
    public ModelAndView resp6(ModelAndView mav){
        //在请求域中添加对象
        mav.addObject("msg","ModelAndView");
        //指定视图页面在哪儿
        mav.setViewName("redirect:/index.jsp");
        return mav;
    }
}
